import Reflux from 'reflux';
import PageConst from './PageConst';
import Actions from './Actions';
const DB = require('../../../app/db');
import Utils from './../../../utils'
import update from 'react-addons-update';
import StorageUtils from './../../../app/storage'
import nattyStorage from 'natty-storage';

const storageEngs = nattyStorage({
  type: 'localStorage',  // 缓存方式, 默认为'localStorage'
  key: 'sskj-dingtalk-engs',     // !!! 唯一必选的参数, 用于内部存储 !!!
});

export default Reflux.createStore({
    listenables: [Actions],
    data: {
        ...PageConst,
      lastPage: false,
      keyword: '',
      currentPage: 1,
      pageSize: 1,
      sgls:[],
      jlls:[],
      aqls:[],
      engid:'',
      //选中的工程
      currentEng: {},
      userInfo: {
        certNum: '',
        telNum: '13972608122',
        vcode: ''
      }

    },

  onSetCurrentEng: function (obj) {
/*    this.data.sgls = update(t.data.sgls, {$set: []});
    this.data.jlls = update(t.data.jlls, {$set: []});
    this.data.aqls = update(t.data.aqls, {$set: []});

    this.data.sgtb = update(this.data.sgtb, {seng: {$set: {}}});
    this.data.sgtb = update(this.data.sgtb, {jlrq: {$set: {}}});
    this.data.jltb = update(this.data.jltb, {seng: {$set: {}}});
    this.data.jltb = update(this.data.jltb, {jlrq: {$set: {}}});
    this.data.aqtb = update(this.data.aqtb, {seng: {$set: {}}});
    this.data.aqtb = update(this.data.aqtb, {jlrq: {$set: {}}});*/

    this.data.currentEng = update(this.data.currentEng, {$set: obj});
    let resultOid = Utils.isEmptyObject(this.data.currentEng.oid);
    if (!resultOid.success)
    {

      this.data.engid = update(this.data.engid, {$set: this.data.currentEng.id});

    }
    else {
      this.data.engid = update(this.data.engid, {$set: this.data.currentEng.oid});

    }


    this.data.engid = update(this.data.engid, {$set: this.data.currentEng.id});

    this.onSgFetch({
      page: 1,
      size: 50,
      keyword:'',
      userid:'13972608122',
      engid:'',
      rq:'',
    });
    this.onJlFetch({
      page: 1,
      size: 50,
      keyword:'',
      userid:'13972608122',
      engid:'',
      rq:'',
    });
    this.onAqFetch({
      page: 1,
      size: 50,
      keyword:'',
      userid:'13972608122',
      engid:'',
      rq:'',
    });
    this.updateComponent();
  },

  onJlInit: function () {
    let t = this;
    t.data.sgls = update(t.data.sgls, {$set: []});
    t.data.jlls = update(t.data.jlls, {$set: []});
    t.data.aqls = update(t.data.aqls, {$set: []});
    this.data.currentEng =  update(this.data.currentEng,  {$set: {}});

    this.data.sgtb = update(this.data.sgtb, {seng: {$set: {}}});
    this.data.sgtb = update(this.data.sgtb, {jlrq: {$set: {}}});
    this.data.jltb = update(this.data.jltb, {seng: {$set: {}}});
    this.data.jltb = update(this.data.jltb, {jlrq: {$set: {}}});
    this.data.aqtb = update(this.data.aqtb, {seng: {$set: {}}});
    this.data.aqtb = update(this.data.aqtb, {jlrq: {$set: {}}});
    this.data.engid = update(this.data.engid, {$set: ''});
    //第一次进入在reflux的状态中无数据才从浏览器缓存中读取
    if (t.data.sgls.length == 0) {
      this.onSgFetch({
        page: 1,
        size: 50,
        keyword: '',
        userid: '13972608122',
        engid: '',
        rq: '',
      });
    }
    if (t.data.jlls.length == 0) {
      this.onJlFetch({
        page: 1,
        size: 50,
        keyword: '',
        userid: '13972608122',
        engid: '',
        rq: '',
      });
    }
    if (t.data.aqls.length == 0) {
        this.onAqFetch({
        page: 1,
        size: 50,
        keyword:'',
        userid:'13972608122',
        engid:'',
        rq:'',
      });

    }
    t.updateComponent();
  },


  onSgFetch: function (params) {
    console.info('onKeyFetch');
    let t = this;
    params.page = t.data.currentPage;
    params.size = t.data.pageSize;
    params.keyword = t.data.keyword;
    params.engid = t.data.engid;
    console.info('参数');
    console.info(params);
    let resultEng = Utils.isEmptyObject(this.data.currentEng);
    if (resultEng.success)
    {
      t.data.sgtb = update(t.data.sgtb, {seng: {$set: this.data.currentEng}});
      t.data.sgtb = update(t.data.sgtb, {jlrq: {$set: {}}});
    }
    DB.WechatAPI.findSGRiZhiControl(params).then(
      function (content) {
        if (content != null) {
          t.data.sgls = update(t.data.sgls, {$set: content.content});
          if(t.data.sgls.length>0)
          {
            console.info('施工1：');
            t.data.sgtb = update(t.data.sgtb, {seng: {$set: t.data.sgls[0].eng}});
            t.data.sgtb = update(t.data.sgtb, {jlrq: {$set: t.data.sgls[0].jlrq}});
          }
          else {
            console.info('施工2：');
            t.data.sgtb = update(t.data.sgtb, {seng: {$set: {}}});
            t.data.sgtb = update(t.data.sgtb, {jlrq: {$set:{}}});
          }
          console.info('转换数据施工2：');
          console.info(t.data.sgtb.seng);

        }
        t.updateComponent();
      },
      function (error) {
        console.log(error);
      });
  },


  onJlFetch: function (params) {

    let t = this;
    params.page = t.data.currentPage;
    params.size = t.data.pageSize;
    params.keyword = t.data.keyword;
    params.engid = t.data.engid;
    let resultEng = Utils.isEmptyObject(this.data.currentEng);
    if (resultEng.success)
    {
      t.data.jltb = update(t.data.jltb, {seng: {$set: this.data.currentEng}});
      t.data.jltb = update(t.data.jltb, {jlrq: {$set: {}}});
    }
    DB.WechatAPI.findJLRiZhiControl(params).then(
      function (content) {
        if (content != null) {
          t.data.jlls = update(t.data.jlls, {$set: content.content});
          if(t.data.jlls.length>0)
          {
            t.data.jltb = update(t.data.jltb, {seng: {$set: t.data.jlls[0].eng}});
            t.data.jltb = update(t.data.jltb, {jlrq: {$set: t.data.jlls[0].jlrq}});
          }
          else {
            t.data.jltb = update(t.data.jltb, {seng: {$set: {}}});
            t.data.jltb = update(t.data.jltb, {jlrq: {$set: {}}});
          }
        }
        t.updateComponent();
      },
      function (error) {
        console.log(error);
      });
  },

  onAqFetch: function (params) {
    let t = this;
    params.page = t.data.currentPage;
    params.size = t.data.pageSize;
    params.keyword = t.data.keyword;
    params.engid = t.data.engid;
    let resultEng = Utils.isEmptyObject(this.data.currentEng);
    if (resultEng.success)
    {
      t.data.aqtb = update(t.data.aqtb, {seng: {$set: this.data.currentEng}});
      t.data.aqtb = update(t.data.aqtb, {jlrq: {$set: {}}});
    }
    DB.WechatAPI.findAQRiZhiControl(params).then(
      function (content) {
        if (content != null) {
          t.data.aqls = update(t.data.aqls, {$set: content.content});
          if(t.data.aqls.length>0)
          {
            t.data.aqtb = update(t.data.aqtb, {seng: {$set: t.data.aqls[0].eng}});
            t.data.aqtb = update(t.data.aqtb, {jlrq: {$set: t.data.aqls[0].jlrq}});
          }
          else {
            t.data.aqtb = update(t.data.aqtb, {seng: {$set: {}}});
            t.data.aqtb = update(t.data.aqtb, {jlrq: {$set: {}}});
          }
        }
        t.updateComponent();
      },
      function (error) {
        console.log(error);
      });
  },



  updateComponent: function () {
        this.trigger(this.data);
  },

  getDefaultProps: function () {

  },

  onFetch: function (params, cb) {
    let t = this;
    t.updateComponent();
    cb && cb(t.data);
  },

  getInitialState: function () {
        return this.data;
  }
});
